package com.xnchat.pro.job.impl;

import cn.hutool.core.lang.Console;
import com.xnchat.pro.job.MessageJob;
import com.xnchat.pro.thread.ClockExecuter;
import com.xnchat.pro.utils.MyDrrrUtil;
import com.xnchat.pro.vo.ClockVo;
import com.xnchat.pro.vo.DelayObject;

import java.util.concurrent.DelayQueue;
import java.util.concurrent.TimeUnit;

/**
 * clock消息线程，用来消费消息
 */
public class ClockJob extends Thread implements MessageJob{
    //这是id，用来区分任务的
    private int  jobid;
    //停止标志位
    private volatile boolean toStop = false;
    //停止原因
    private String stopReason;
    //消息队列统一推送
    public static DelayQueue<DelayObject<ClockVo>> xiaban=new DelayQueue<>();

    @Override
    public void exec() {
        System.out.println("测试执行");
        int count = 0;
            MyDrrrUtil send=new MyDrrrUtil();
            while (!toStop){
                count++;
                try {
                DelayObject<ClockVo> poll = xiaban.poll(3, TimeUnit.SECONDS);
                if(poll!=null){
                    count=0;
                    ClockVo v = poll.getT();
                    send.sendMessage(v.getSession(),v.getMessage());
                }else {
                    if(count>30&&xiaban.size()<=0){
                        break;
                    }
                }
                }catch (Exception e ){
                    if (toStop){
                        xiaban.clear();
                        Console.error("手动终止：",e);
                    }else {
                        Console.error("未知错误",e);
                    }
                }

            }
            xiaban.clear();
        ClockExecuter.removeThread(jobid,"job 【下班进程】停止");

    }

    /**
     * time >2
     * @param name
     * @param session
     * @param id
     */
    public ClockJob (String name,int time ,String session,int id){
        time-=1;
        for(int i=1;i<time;i++){
            ClockVo clockVo=new ClockVo(name+" 距离下班还有[" + i + "]分钟",session,name);
            xiaban.add(new DelayObject<>((time-i)* 60*1000, clockVo));
        }
        ClockVo clockVo=new ClockVo(name+"辛苦啦~，明天见",session,name);
        xiaban.add(new DelayObject<>(time* 60*1000, clockVo,0));
        this.jobid=id;
    }

    public ClockJob addJob(String name,int time ,String session){
        time+=1;
        for(int i=1;i<time-1;i++){
            ClockVo clockVo=new ClockVo(name+" 距离下班还有[" + i + "]分钟",session,name);
            xiaban.add(new DelayObject<>((time-i)* 60*1000, clockVo));
        }
        ClockVo clockVo=new ClockVo(name+"辛苦啦~，明天见",session,name);
        xiaban.add(new DelayObject<>(time* 60*1000, clockVo,0));
        return this;
    }

    @Override
    public void toStop(String stopReason) {
        /**
         * Thread.interrupt只支持终止线程的阻塞状态(wait、join、sleep)，
         * 在阻塞出抛出InterruptedException异常,但是并不会终止运行的线程本身；
         * 所以需要注意，此处彻底销毁本线程，需要通过共享变量方式；
         */
        this.toStop = true;
        this.stopReason = stopReason;

    }


    @Override
    public void run() {
            exec();
    }
}
